﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace BYManageInfoSystem
{
    public partial class Frm_FinChuKouJinESet : DockContent
    {

        private DGBY.BLL.FactoryGoodsTab m_bllFactoryGoodsTab = new DGBY.BLL.FactoryGoodsTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.FinChuKouJinETab_New m_bllFinChuKouJinETab_New = new DGBY.BLL.FinChuKouJinETab_New();

        public Frm_FinChuKouJinESet()
        {
            InitializeComponent();
        }
        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllFinChuKouJinETab_New.GetFinInvoiceRecordTabInfo(strWhere);
            ultraGrid1.DataSource = null;

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void AppendFinChuKouJinETab_New()
        {
            DataSet ds = m_bllFinChuKouJinETab_New.SyncFinChuKouJinETab_NewInfo("FinChuKouJinETab_New.PaidanRegisterTabID is null");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nPaiDanID = (int)ds.Tables[0].Rows[i]["报关派单ID"];
                    string strBizhi = ds.Tables[0].Rows[i]["币制"].ToString();
                    Decimal dtTemp = (Decimal)ds.Tables[0].Rows[i]["报关金额"];

                    Decimal dtSetPercent = 0;
                    DataSet dsInfo = m_bllFactoryGoodsTab.GetGoodsBandInfo("PaidanRegisterTab.PaidanRegisterTabID = '"
            + nPaiDanID.ToString() + "'");
                    if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
                    {
                        dtSetPercent = Convert.ToDecimal(dsInfo.Tables[0].Rows[0]["设百分比"].ToString());
                    }

                    DGBY.Model.FinChuKouJinETab_New modelFinChuKouJinETab_New = new DGBY.Model.FinChuKouJinETab_New();
                    modelFinChuKouJinETab_New.PaidanRegisterTabID = nPaiDanID;
                    modelFinChuKouJinETab_New.USDHuiLv = 0;
                    modelFinChuKouJinETab_New.HKHuiLv = 0;
                    modelFinChuKouJinETab_New.HKPercent17 = 0;
                    modelFinChuKouJinETab_New.HKPercent16 = 0;
                    modelFinChuKouJinETab_New.HKPercent15 = 0;
                    modelFinChuKouJinETab_New.HKPercent13 = 0;
                    modelFinChuKouJinETab_New.HKPercent9 = 0;
                    modelFinChuKouJinETab_New.HKPercent5 = 0;
                    modelFinChuKouJinETab_New.USDPercent17 = 0;
                    modelFinChuKouJinETab_New.USDPercent16 = 0;
                    modelFinChuKouJinETab_New.USDPercent15 = 0;
                    modelFinChuKouJinETab_New.USDPercent13 = 0;
                    modelFinChuKouJinETab_New.USDPercent9 = 0;
                    modelFinChuKouJinETab_New.USDPercent5 = 0;

                    if (strBizhi == "港币" && dtSetPercent == 0.17M)
                    {
                        modelFinChuKouJinETab_New.HKPercent17 = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.16M)
                    {
                        modelFinChuKouJinETab_New.HKPercent16 = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.15M)
                    {
                        modelFinChuKouJinETab_New.HKPercent15 = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.13M)
                    {
                        modelFinChuKouJinETab_New.HKPercent13 = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.09M)
                    {
                        modelFinChuKouJinETab_New.HKPercent9 = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.05M)
                    {
                        modelFinChuKouJinETab_New.HKPercent5 = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.17M)
                    {
                        modelFinChuKouJinETab_New.USDPercent17 = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.16M)
                    {
                        modelFinChuKouJinETab_New.USDPercent16 = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.15M)
                    {
                        modelFinChuKouJinETab_New.USDPercent15 = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.13M)
                    {
                        modelFinChuKouJinETab_New.USDPercent13 = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.09M)
                    {
                        modelFinChuKouJinETab_New.USDPercent9 = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.05M)
                    {
                        modelFinChuKouJinETab_New.USDPercent5 = dtTemp;
                    }

                    m_bllFinChuKouJinETab_New.Add(modelFinChuKouJinETab_New);

                }
            }
        }

        private void Frm_FinChuKouJinESet_Load(object sender, EventArgs e)
        {
            AppendFinChuKouJinETab_New();
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.LocalPrintPreveiw();
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "报关金额" || str == "合同号" || str == "币制"
                    || str == "经营公司" || str == "申报日期")
                {
                    column2.CellAppearance.BackColor = Color.Coral;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }
        }

        private void buttonBacthMod_Click(object sender, EventArgs e)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            if (ugr.Length <= 0)
            {
                MessageBox.Show("没有要批量修改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            for (int i = 0; i < ugr.Length; i++)
            {
                int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                DGBY.Model.FinChuKouJinETab_New modelFinChuKouJinETab_New
                    = m_bllFinChuKouJinETab_New.GetModel(nID);

                modelFinChuKouJinETab_New.USDHuiLv = Convert.ToDecimal(this.textBoxUsdHuiLv.Text);
                modelFinChuKouJinETab_New.HKHuiLv = Convert.ToDecimal(this.textBoxHKHuiLv.Text);
                m_bllFinChuKouJinETab_New.Update(modelFinChuKouJinETab_New);
            }


            MessageBox.Show("批量修改汇率成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FinChuKouJinETab_New modelFinChuKouJinETab_New = m_bllFinChuKouJinETab_New.GetModel(nID); 
                string strContractNo = CurrentUGR.Cells["合同号"].Value.ToString();


                if (ClassInputValid.IsNum(strContractNo))
                {
                    modelFinChuKouJinETab_New.PaidanRegisterTabID = Convert.ToInt32(CurrentUGR.Cells["合同号"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTab.ContractCode = '" + strContractNo + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFinChuKouJinETab_New.PaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                    }
                }

                //modelFinChuKouJinETab_New.USDJinE = Convert.ToDecimal(CurrentUGR.Cells["USD金额"].Value.ToString());
                //modelFinChuKouJinETab_New.SetPercent = Convert.ToDecimal(CurrentUGR.Cells["设百分比"].Value.ToString());
                modelFinChuKouJinETab_New.USDPercent17 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设17"].Value.ToString());
                modelFinChuKouJinETab_New.USDPercent16 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设16"].Value.ToString());
                modelFinChuKouJinETab_New.USDPercent15 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设15"].Value.ToString());
                modelFinChuKouJinETab_New.USDPercent13 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设13"].Value.ToString());
                modelFinChuKouJinETab_New.USDPercent9 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设9"].Value.ToString());
                modelFinChuKouJinETab_New.USDPercent5 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设5"].Value.ToString());

                modelFinChuKouJinETab_New.HKPercent17 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设17"].Value.ToString());
                modelFinChuKouJinETab_New.HKPercent16 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设16"].Value.ToString());
                modelFinChuKouJinETab_New.HKPercent15 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设15"].Value.ToString());
                modelFinChuKouJinETab_New.HKPercent13 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设13"].Value.ToString());
                modelFinChuKouJinETab_New.HKPercent9 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设9"].Value.ToString());
                modelFinChuKouJinETab_New.HKPercent5 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设5"].Value.ToString());

                modelFinChuKouJinETab_New.USDHuiLv = Convert.ToDecimal(CurrentUGR.Cells["美元汇率"].Value.ToString());
                modelFinChuKouJinETab_New.HKHuiLv = Convert.ToDecimal(CurrentUGR.Cells["港币汇率"].Value.ToString());

                m_bllFinChuKouJinETab_New.Update(modelFinChuKouJinETab_New);

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                ModifyGoodsDetail(e.Row.Index);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }
    }
}
